﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace PanasonicWarranty.UI
{
    public partial class KPIReport : BasePage
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            pagingtop.PagingChange += new EventHandler(paging_PagingChange);
            pagingbottom.PagingChange += new EventHandler(paging_PagingChange);
        }

        void paging_PagingChange(object sender, EventArgs e)
        {
            pagingtop.PagingChange -= new EventHandler(paging_PagingChange);
            pagingbottom.PagingChange -= new EventHandler(paging_PagingChange);
            pagingtop.CurrentPage = (sender as UserControl.ucPaging).CurrentPage;
            pagingbottom.CurrentPage = (sender as UserControl.ucPaging).CurrentPage;
            BindDataReport();
            pagingtop.Visible = pagingbottom.Visible = (sender as UserControl.ucPaging).TotalPage > 1;
            pagingtop.PagingChange += new EventHandler(paging_PagingChange);
            pagingbottom.PagingChange += new EventHandler(paging_PagingChange);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PageSize = Convert.ToInt32(tSortable_length.SelectedValue);
                pagingtop.CurrentPage = pagingbottom.CurrentPage = 1;
                pagingtop.PageSize = pagingbottom.PageSize = PageSize;
                pagingtop.PageDisplay = pagingbottom.PageDisplay = 6;
                BindDataReport();
            }
        }

        private void BindDataReport()
        {
            try
            {
                string subcriber = null;
                DateTime? fromTime = null;
                DateTime? toTime = null;
                int? currentPage = pagingtop.CurrentPage;
                int? pageSize = pagingtop.PageSize;
                int? totalRow = 0;
                if (!string.IsNullOrEmpty(txtPhoneNumber.Text.Trim())) subcriber = txtPhoneNumber.Text.Trim();
                if (!string.IsNullOrEmpty(txtFromDate.Text.Trim())) fromTime = Convert.ToDateTime(txtFromDate.Text.Trim());
                if (!string.IsNullOrEmpty(txtToDate.Text.Trim())) toTime = Convert.ToDateTime(txtToDate.Text.Trim()).AddDays(1);
                rptStatistic.DataSource = DB.sp_pepsi_KPIReport(subcriber, fromTime, toTime, currentPage, pageSize, ref totalRow)
                                          .Select(p => new
                                          {
                                              p.subscriber,
                                              p.kpi_value,
                                              p.report_time,
                                              p.mo_guid,
                                              kpi_name = DB.PepsiKPIs.FirstOrDefault(q => q.kpi_id == p.kpi_id).kpi_name
                                          });
                pagingtop.TotalRecord = pagingbottom.TotalRecord = totalRow.Value;
                pagingtop.Visible = pagingbottom.Visible = pagingtop.TotalPage > 1;
                rptStatistic.DataBind();
                ltrInfo.Text = "Total pages: " + pagingtop.TotalPage; 
            }
            catch { }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            pagingtop.PagingChange -= new EventHandler(paging_PagingChange);
            pagingbottom.PagingChange -= new EventHandler(paging_PagingChange);
            pagingtop.CurrentPage = pagingbottom.CurrentPage = 1;
            pagingtop.PagingChange += new EventHandler(paging_PagingChange);
            pagingbottom.PagingChange += new EventHandler(paging_PagingChange);
            BindDataReport();
        }

        protected void rptStatistic_ItemCreated(object sender, RepeaterItemEventArgs e)
        {
            try
            {
                HtmlTableRow row = (HtmlTableRow)e.Item.FindControl("itemRow");
                row.Attributes.Add("onmouseover", "$(this).addClass('hover')");
                row.Attributes.Add("onmouseout", "$(this).removeClass('hover')");
                if (e.Item.ItemIndex % 2 != 0)
                    row.Attributes.Add("class", "odd");
            }
            catch { }
        }

        protected void tSortable_length_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                PageSize = Convert.ToInt32(tSortable_length.SelectedValue);
                pagingtop.PageSize = pagingbottom.PageSize = PageSize;
                BindDataReport();
            }
            catch { }
        }
    }
}